//
//  JJOAuthViewModel.swift
//  JJSina
//
//  Created by mac on 2017/12/6.
//  Copyright © 2017年 xinyuan. All rights reserved.
//

import UIKit
// 用viewmodel请求数据
// 保存数据提供数据给其他控制器
class JJOAuthViewModel: NSObject {

    // 全局访问点
    static let shared: JJOAuthViewModel = JJOAuthViewModel()
    
    var userAccountModel: JJUserAccountModel?
    // 判断是否过期
    var accessToken: String? {
        if userAccountModel?.expires_Date?.compare(Date()) == ComparisonResult.orderedDescending {
            return userAccountModel?.access_token
        } else {
            return nil
        }
    }
    // 判断用户是否登录
    var isLogin: Bool {
        return accessToken != nil
    }
    override init() {
        super.init()
        userAccountModel = getUserAccountModel()
    }
    
}
extension JJOAuthViewModel {
    // 保存模型
    func saveUserAccountModel(userAccountModel: JJUserAccountModel) {
        self.userAccountModel = userAccountModel
        let file = (NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last! as NSString).appendingPathComponent("userAccount.archiver")
        NSKeyedArchiver.archiveRootObject(userAccountModel, toFile: file)
    }
    // 获取模型
    func getUserAccountModel()->JJUserAccountModel? {
        let file = (NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last! as NSString).appendingPathComponent("userAccount.archiver")
        let userAccountModel = NSKeyedUnarchiver.unarchiveObject(withFile: file) as?JJUserAccountModel
        
        return userAccountModel
    }
}
extension JJOAuthViewModel {
    // 获取accessTOKEN的方法
    func getUserAccount(code: String, isFinish:@escaping (Bool)->()) {
        
        JJNetworkTools.shared.loadUserAccount(code: code, success: { (response) in
            guard let res = response as? [String: Any] else {
                isFinish(false)
                return
            }
//            print(res)
            let userAccountModel = JJUserAccountModel.yy_model(withJSON: res)
            guard let userAM = userAccountModel else {
                isFinish(false)
                return
            }
//            print(userAM)
            self.getUserInfo(userAccountModel: userAM, isFinish: isFinish)
            
            
        }) { (error) in
            isFinish(false)
            print(error)
        }
    }
    
    // 获取用户信息
    func getUserInfo(userAccountModel: JJUserAccountModel, isFinish:@escaping (Bool)->()) {
        JJNetworkTools.shared.loadUserInfo(userAccountModel: userAccountModel, success: { (response) in
            guard let res = response as? [String: Any] else {
                isFinish(false)
                return
            }
            userAccountModel.screen_name = res["screen_name"] as? String
            userAccountModel.avatar_large = res["avatar_large"] as? String
//            print(userAccountModel.access_token, userAccountModel.avatar_large)
            // 保存用户模型
            self.saveUserAccountModel(userAccountModel: userAccountModel)
            isFinish(true)
        }) { (error) in
            isFinish(false)
            print(error)
        }
    }
}
